<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/buttons.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/alert.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/cart.css"/>
    <script src="../../static/js-tuangou/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/js-tuangou/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
    <title>用户充值</title>
    <!-- nav导航条 -->
    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">

            <div class="navbar-header">
                <a href="le-index.html" class="navbar-brand">
                    <span class="glyphicon glyphicon-home le-text"></span>
                    <span class="le-text">乐购商城</span>
                </a>

                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#le-nav">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

            </div>

            <div class="navbar-collapse collapse" id="le-nav">
                <ul class="navbar-nav nav hidden-xs">
                    <li><a href="">乐购 惊喜 盛大开业 欢迎来购</a></li>
                </ul>

                <ul class="navbar-nav nav navbar-right">
                    <li><a href="le-index.html">关于乐购</a></li>
                    <li><a href="le-mygroupbying.html">我的拼团</a></li>
                    <li><a href="le-cart.html">购物车</a></li>
                    <li><a href="le-balance.html">充值</a></li>
                    <li><a href="le-list.html">用户信息</a></li>


                    <!-- 当没有登录时 点击会跳转到登录页面 -->

                    <!-- 下拉列表阻止a超链接的默认行为 -->
                    <li class="dropdown">
                        <a data-toggle="modal" data-target="#loginModal" class="dropdown-toggle" id="le-login">登录</a>
                        <!-- 下拉菜单 -->
                        <ul class="dropdown-menu">
                            <!--                        <li><a href="le-personal.html">个人中心</a></li>-->
                            <!--                        <li><a href="" id="update-pwd" data-toggle="modal" data-target="#bt-modal2">修改密码</a></li>-->
                            <li><a href="" id="le-user-logout" data-toggle="modal" data-target="#bt-modal">退出</a></li>
                        </ul>
                    </li>
                    <li><a data-toggle="modal" data-target="#registerModal">注册</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="le-top-ad">
        <div class="container le-ad-bg">
            <span class="close">&times;</span>
        </div>
    </div>
    <!-- End of empty space -->

    <!-- 引入 Layui 的样式文件 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.6.8/css/layui.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .layui-container {
            margin-top: 30px;
        }
    </style>
</head>

<body>

<div class="layui-container">
    <form class="layui-form" id="rechargeForm">
        <div class="layui-form-item">
            <label class="layui-form-label" for="userId">用户ID：</label>
            <div class="layui-input-inline">
                <input type="number" id="userId" name="userId" lay-verify="required" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" for="amount">金额：</label>
            <div class="layui-input-inline">
                <input type="number" id="amount" name="amount" step="0.01" lay-verify="required|number"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit" style="background-color: #a71d2a;">充值</button>
            </div>
        </div>
    </form>
    <div class="layui-form-item layui-form-text" id="resultMessage"></div>

    <table class="layui-table" lay-skin="line" lay-size="lg" id="userData">
        <thead>
        <tr>
            <th>当前用户ID的</th>
            <th>当前余额</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td id="userIdData"></td>
            <td id="balanceData"></td>
        </tr>
        </tbody>
    </table>

    <a href="/le-index" class="layui-btn layui-btn-primary">返回首页</a>
</div>

<!-- 引入 Layui 的 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/layui/2.6.8/layui.min.js"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.jquery;

        // 监听表单提交
        form.on('submit(formSubmit)', function (data) {
            var userId = $("#userId").val();
            var amount = $("#amount").val();

            $.ajax({
                type: "POST",
                url: "/tbUser/recharge/" + userId + "?amount=" + amount,
                contentType: "application/json",
                success: function (response) {
                    $("#resultMessage").text(response.message);
                    // 更新余额
                    updateUserData(response.data.userId, response.data.balance);
                },
                error: function (xhr, status, error) {
                    $("#resultMessage").text("错误：" + xhr.statusText);
                }
            });

            return false; // 阻止表单跳转
        });

        // 更新用户数据
        function updateUserData(userId, balance) {
            $("#balanceData").text(balance);
        }
    });
</script>
</body>

</html>